#include "modelT.hpp"
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <GL/glut.h> 

/*
	OBSELETE reprendre les amélioration de modele
*/

using namespace std;

modelTextured::modelTextured(){
	nbPoints = 0;
	nbTexs=0;
	nbNodes = 0;
	this->fileText = "None";
}

modelTextured::modelTextured(unsigned int sizeP,unsigned int sizeT,unsigned int sizeN, string fileText){
	nbPoints = sizeP;
	nbTexs = sizeT;
	nbNodes = sizeN;
	this->fileText = fileText;

	points = Matrix<double>(4,nbPoints);
	texs = Matrix<double>(3,nbTexs);
	nodes = Matrix<int>(6,nbNodes);
}

modelTextured::~modelTextured(){
}

modelTextured* modelTextured::importFromFile(string file){
	ifstream fichier(file.c_str(), ios::in);
	modelTextured *m = NULL;

	if(fichier){
		unsigned int nbPoint, nbTex, nbNoeud;
		string fileText;
		char c;		//récupération de la parenthese
		
		fichier >> nbPoint >>c>> nbTex >>c>> nbNoeud >> fileText;

		m = new modelTextured(nbPoint,nbTex,nbNoeud,fileText);

		double tabP[nbPoint*4],tabT[nbPoint*3];
		int tabN[nbNoeud*6];
		unsigned int j;

		j=0;
		for(unsigned int i = 0; i<nbPoint; i++){

			//TODO: debug use with matrix::operator[]
			//fichier>>(*(m->points)[i][0])>>(*(m->points)[i][1])>>(*(m->points)[i][2]);
			//(*(m->points)[i][3]) = 0;

			fichier>>tabP[j];	j++;
			fichier>>tabP[j];	j++;
			fichier>>tabP[j];	j++;
			tabP[j] = 0;		j++;

		}
		m->points.fill(tabP);

		j=0;
		for(unsigned int i = 0; i<nbTex; i++){
			fichier>>tabT[j];	j++;
			fichier>>tabT[j];	j++;
			fichier>>tabT[j];	j++;
		}
		m->texs.fill(tabT);

		for(unsigned int i = 0; i<nbNoeud; i++){
			fichier>>tabN[i*nbNoeud];
			fichier>>c>>tabN[i*nbNoeud+3]>>c;
			fichier>>tabN[i*nbNoeud+1];
			fichier>>c>>tabN[i*nbNoeud+4]>>c;
			fichier>>tabN[i*nbNoeud+2];
			fichier>>c>>tabN[i*nbNoeud+5]>>c;
		}
		m->nodes.fill(tabN);

		fichier.close();
	}
	else{ 
		cerr<<"Erreur ouverture de "<<file<<endl;
	}

	return m;
}

void modelTextured::print() const {
	cout<<"Points "<<nbPoints<<endl;
	cout<<"Texts "<<nbTexs<<endl;
	cout<<"Nodes "<<nbNodes<<endl;
	if(nbNodes<1000)
		nodes.print();

}

void modelTextured::applyMatrix(Matrix<double> &m){
	points *= m;
}

void modelTextured::drawPixel(int x, int y){
	glBegin(GL_POINTS);
	glVertex2f(x,y);
	glEnd();
}

void modelTextured::drawLine(double x1, double y1, double x2, double y2){
	if(x2>x1){
		double tx,ty;
		tx = x1;
		ty = y1;
		x1 = x2;
		x1 = x2;
		x2 = tx;
		x2 = ty;
	}

	double dx = x2 - x1;
	double dy = y2 - y1;

	if(dx>dy){
		double t;
		t = x1;
		x1 = y1;
		y1 = t;
		dx = x2 - x1;
		dy = y2 - y1;
	}

	double inCE = 2 * dy;
	double inCNE = 2 * dy - 2 * dx;
	double d = 2 * dy - dx;
	double y = y1;

	for(int x = x1; x<= x2 ; x++){
		drawPixel(x,y);
		if(d<=0){
			d += inCE;
		}
		else{
			d += inCNE;
			y++;
		}
	}

}

void modelTextured::draw1(){
	glClear(GL_COLOR_BUFFER_BIT);
	glColor3f(0.0f,0.0f,0.0f);
	for(unsigned int  i = 0; i<nbNodes ; i++){
		int p1,p2,p3;
		p1 = nodes[i][0];
		p2 = nodes[i][1];
		p3 = nodes[i][2];
		drawLine(points[p1][0],points[p1][1],points[p2][0],points[p2][1]);
		drawLine(points[p2][0],points[p2][1],points[p3][0],points[p3][1]);
		drawLine(points[p3][0],points[p3][1],points[p1][0],points[p1][1]);
	}
}

